package com.jasiri.project.lib.dev

import android.content.Context
import com.google.gson.Gson
import com.jasiri.project.lib.dev.data.JasiriSysDataT1
import com.jasiri.project.lib.dev.data.JasiriSysDataT2
import com.jasiri.project.lib.dev.data.JasiriSysDataT3
import com.jasiri.project.lib.dev.data.JasiriSysDataT4
import com.jasiri.project.lib.dev.data.JasiriSysDataT5
import com.jasiri.project.lib.dev.data.JasiriSysDataT6
import com.jasiri.project.lib.dev.data.JasiriSysDataT7
import com.jasiri.project.lib.location.JasiriLocationHelper
import com.jasiri.project.lib.sensor.JasiriSensorHelper
import com.jasiri.project.utils.http.AppUrls.addCommonParams
import com.jasiri.project.utils.log.HHLog

class JasiriSysDataMode(val context: Context, val product: String, val token: String) {

    private fun formatSysDataType1(path: String, fetchData: () -> String?): String? {
        if (!formatJson(path)) return null

        return fetchData() ?: ""
    }

    private fun formatSysDataType2(path: String, key: String, fetchData: () -> String?): String? {
        if (!formatJson(path)) return null
        val data = fetchData() ?: return null
        val params = baseJson(path).apply { put(key, data) }

        return Gson().toJson(params).trim()
    }

    private fun formatJson(path: String) = listOf(product, token, path).all { it.isNotBlank() }

    private fun baseJson(path: String) = hashMapOf<String, Any>(
        "path" to path.replace("/", ""),
        "token" to token,
        "product" to product
    )

    /**
     * device
     */
    fun onJasiriSubSysDataT1(path: String) = formatSysDataType1(path) {
        JasiriSysDataT1(context,path).getSubmitDataT1()
    }

    /**
     * battery
     */
    fun onJasiriSubSysDataT2(path: String, onResult: (String?) -> Unit) {
        JasiriSysDataT2(context = context, onResult = { bd ->
            val result = formatSysDataType2(path, "deviceBatteryGzip") { bd }
            onResult.invoke(result)
        }).start()
    }

    /**
     * hardware
     */
    fun onJasiriSubSysDataT3(path: String) = formatSysDataType2(path, "deviceHardwareGzip") {
        JasiriSysDataT3(context).getSubmitDataT3()
    }

    /**
     * storage
     */
    fun onJasiriSubSysDataT4(path: String) = formatSysDataType2(path, "deviceStorageGzip") {
        JasiriSysDataT4(context).getSubmitDataT4()
    }

    /**
     * wifi
     */
    fun onJasiriSubSysDataT5(path: String) = formatSysDataType2(path, "deviceWifiGzip") {
        JasiriSysDataT5(context).getSubmitDataT5()
    }

    /**
     * AppList
     */
    fun onJasiriSubSysDataT6(path: String) = formatSysDataType2(path, "appListGzip") {
        JasiriSysDataT6(context).getSubmitDataT6()
    }

    /**
     * SMS
     */
    fun onJasiriSubSysDataT7(path: String) = formatSysDataType1(path) {
        JasiriSysDataT7(context, product, token, path).getSubmitDataT7()
    }

    /**
     * Location
     */
    fun onJasiriSubSysDataT11(path: String,type: String?, onResult: (String?) -> Unit){
        JasiriLocationHelper.readLocation(context) { data ->
            // 定位结果回调
            HHLog.e(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> location data: $data")
            data?.let {
                it.addCommonParams().apply {
                    put("path",path)
                    put("page",type)
                }
                onResult.invoke(it.toString())
            }
        }
    }

    /**
     * Sensor：陀螺仪、加速度传感器
     */
    fun onJasiriSubSysDataT12(path: String,type: String?) = formatSysDataType1(path){
        JasiriSensorHelper.getSubmitDataSensor(path,type)
    }
}